package com.ruoyi.web.controller.mes;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.kt.mes.util.WebSocketServer;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.kt.mes.domain.OrderProduct;
import com.kt.mes.service.IOrderProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产订单Controller
 *
 * @author lzc
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/mes/order_product")
public class OrderProductController extends BaseController
{
    @Autowired
    private IOrderProductService orderProductService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询生产订单列表
     */
    @PreAuthorize("@ss.hasPermi('mes:order_product:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderProduct orderProduct)
    {
        startPage();
        //发送给大屏 数据
        List<OrderProduct> list = orderProductService.selectOrderProductList(orderProduct);
        return getDataTable(list);
    }

    /**
     * 导出生产订单列表
     */
    @PreAuthorize("@ss.hasPermi('mes:order_product:export')")
    @Log(title = "生产订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OrderProduct orderProduct)
    {
        List<OrderProduct> list = orderProductService.selectOrderProductList(orderProduct);
        ExcelUtil<OrderProduct> util = new ExcelUtil<OrderProduct>(OrderProduct.class);
        return util.exportExcel(list, "order_product");
    }

    /**
     * 获取生产订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:order_product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(orderProductService.selectOrderProductById(id));
    }

    /**
     * 新增生产订单
     */
    @PreAuthorize("@ss.hasPermi('mes:order_product:add')")
    @Log(title = "生产订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderProduct orderProduct)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        orderProduct.setCreateBy( loginUser.getUser().getNickName());

        return toAjax(orderProductService.insertOrderProduct(orderProduct));
    }

    /**
     * 修改生产订单
     */
    @PreAuthorize("@ss.hasPermi('mes:order_product:edit')")
    @Log(title = "生产订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderProduct orderProduct)
    {
        return toAjax(orderProductService.updateOrderProduct(orderProduct));
    }

    /**
     * 删除生产订单
     */
    @PreAuthorize("@ss.hasPermi('mes:order_product:remove')")
    @Log(title = "生产订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(orderProductService.deleteOrderProductById(id));
    }
}
